﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command.AppaLL;
using AuthCenter_HuangJingtao.Read.Api.Dto;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AuthCenter_HuangJingtao.Read.Api.Application.CommandHandler.AppHandler
{
    public class ShowCommandHandler : IRequestHandler<ShowCommand, ApiResult<List<ShowDto>>>
    {

        IBaseReponsitory<Addresses> Addresses;
        IBaseReponsitory<Citys> Citys;
        IBaseReponsitory<Xians> Xians;

        IBaseReponsitory<Roles> Roles;
        IBaseReponsitory<App> Apps;
        IBaseReponsitory<RoleApp> RoleApp;

        public ShowCommandHandler(IBaseReponsitory<App> Apps, IBaseReponsitory<Addresses> Addresses, IBaseReponsitory<Citys> Citys, IBaseReponsitory<Xians> Xians, IBaseReponsitory<Roles> Roles, IBaseReponsitory<RoleApp> RoleApp)
        {
            this.Apps = Apps;
            this.Addresses = Addresses;
            this.Citys = Citys;
            this.Xians = Xians;

            this.Roles = Roles;
            this.Apps = Apps;
            this.RoleApp = RoleApp;
        }


        public async Task<ApiResult<List<ShowDto>>> Handle(ShowCommand request, CancellationToken cancellationToken)
        {
            var list = from a in Apps.GetQuery()
                       join b in
                      Addresses.GetQuery() on a.AddressId equals b.AddressId
                       join c in Citys.GetQuery() on a.CityId equals c.CityId
                       join d in Xians.GetQuery() on a.XianId equals d.XianId

                       join e in RoleApp.GetQuery() on a.App_Id equals e.App_Id
                       join f in Roles.GetQuery() on e.Role_Id equals f.Role_Id

                       select new ShowDto
                       {
                           App_Id = a.App_Id,
                           App_Name = a.App_Name,
                           App_Sex = a.App_Sex,
                           AddressId = a.AddressId,
                           AddressName = b.Address_Name,
                           CityId = c.CityId,
                           CityName = c.CityName,
                           XianId = d.XianId,
                           XianName = d.Xian_Name,
                           App_Discription = a.App_Discription,
                           App_People = a.App_People,
                           App_Time = a.App_Time,
                           EditPerson = a.EditPerson,
                           EditTime = a.EditTime,
                           Role_Name = "",//所有角色名称
                           Role_Id = e.Role_Id,
                           Role_ArrId = a.Role_ArrId,
                           Group_Id = a.Group_Id,
                           App_IsDel = a.App_IsDel
                       };
            list = list.Where(x => !x.App_IsDel);
            var data = await list.ToListAsync();

   

            foreach (var item in data)
            {
                var role = item.Role_ArrId;
                //把request.App_Id的值[1,2,3]转换成int []并且做循环
                int[] app_ids = role.Split(',').Select(int.Parse).ToArray();
                foreach (var app_id in app_ids)
                {
                    var role_name = Roles.GetQuery().Where(x => x.Role_Id == app_id).Select(x => x.Role_Name).FirstOrDefault();
                    item.Role_Name += role_name + "、";
                }
                item.Role_Name = item.Role_Name.TrimEnd('、');
            }
             data = data.OrderByDescending(x => x.App_Id).ToList();
            return new ApiResult<List<ShowDto>>
            {
                Code = ApiEnum.Success,
                Message = "获取成功",
                Data = data
            };
        }
    }
}
